<?php
namespace app\qq\controller;

use tp5_lib\qq\utility\QQOAuth;
use tp5_lib\qq\model\QQUserModel;
use tp5_lib\user\extension\OutsideController;

/**
 * QQ第三方登录
 */
class IndexController extends OutsideController
{
    public function __get($name)
    {
        if($name === "outside"){
            if(empty($this->_outside)){
                $this->_outside=new QQUserModel();
            }
            return $this->_outside;
        }
        return parent::__get($name);
    }
    public $appid="101561571";
    public $appKey="26f537a7f1a26efb4c057d30c8bd7054";
    public $redirect_uri="http://tp5.1620428040.xyz/qq/index/receivecode";
    public function index()
    {
        session("qq_login_redirect_url",input("redirect_url",""));
        $is_mobile = $this->request->isMobile();
        $state=rand(100000,999999);
        session("qq_auth_state",$state);
        $scope="get_user_info,list_album,upload_pic,do_like";
        $url=QQOAuth::alloc($this->appid,$this->appKey,$this->redirect_uri)->getServerSideAuthUrl($scope,$state,$is_mobile);
        $this->redirect($url);
    }
    public function receiveCode()
    {
        $code=input("code");
        $state=input("state");
        if(session("qq_auth_state") != $state){
            $this->error("验证失败");
        }
        $qq=QQOAuth::alloc($this->appid,$this->appKey,$this->redirect_uri);
        $token_info=$qq->getAccessToken($code);
        if(!$token_info){
            $this->error($qq->getError());
        }
        $openid_info=$qq->getOpenid($token_info["access_token"]);
        $user_info=$qq->getUserInfo($openid_info["openid"],$token_info["access_token"]);
        $model=new QQUserModel();
        if(isset($user_info["unionid"])){
            $record=$model->db()->where(["unionid"=>$openid_info["unionid"]])->find();
        }
        else{
            $record=$model->db()->where(["openid"=>$openid_info["openid"]])->find();
        }
        if($record){
            $model->db()->where(["id"=>$record["id"]])->strict(false)->update($user_info);
            if($record["user_id"]){
                $user=$this->logic->doLogin($record["user_id"]);
                $this->loginRedirect($user);
            }
            else{
                $this->redirect("bindAccount",["id"=>$record["id"]]);
            }
        }
        else{
            $data=array_merge($user_info,$openid_info);
            $id=$model->db()->strict(false)->insertGetId($data);
            $this->redirect("bindAccount",["id"=>$id]);
        }
    }
    protected function convertUserInfo($data)
    {
        $sex = 0;
        if($data["gender"] === "m"){
            $sex = 1;
        }
        elseif($data["gender"] === "w"){
            $sex = 2;
        }
        $user=[
            "username"=>"qq_".time(),
            "mobile"=>"",
            "email"=>"",
            "nickname"=>$data["nickname"],
            "avatar"=>$data["figureurl"],
            "signature"=>"",
            "sex"=>$sex,
            "birthday"=>"",
            "url"=>"",
        ];
        return $user;
    }
}
